#include <elastos.h>
_ELASTOS_NAMESPACE_USING
#if defined(_wince)
#include <aura.h>
#else
extern "C" void * __cdecl ela_malloc(size_t size);
extern "C" void * __cdecl ela_realloc(void* p, size_t newSize);
extern "C" void   __cdecl ela_free(void * p);
#endif

ELAPI_(PVoid) _CMemoryHeap_Alloc(MemorySize size, Boolean zeroMem)
{
    if ((Int32)size < 0) return NULL;
    PVoid pReqMem;
#if defined(_wince)
    pReqMem = (PVoid)Aura_malloc((Aura_Int32)size);
#else
    pReqMem = ela_malloc(size);
#endif
    if (zeroMem && NULL != pReqMem) {
        // TODO: Need optimizing?
        memset(pReqMem, 0, size);
    }
    return pReqMem;
}

ELAPI_(PVoid) _CMemoryHeap_Realloc(PVoid pMem, MemorySize size)
{
    if ((Int32)size < 0) return NULL;
#if defined(_wince)
    return (PVoid)Aura_realloc((Aura_PVoid)pMem, (Aura_Int32)size);
#else
    return ela_realloc(pMem, size);
#endif
}

ELAPI_(void) _CMemoryHeap_Free(void *pMem)
{
#if defined(_wince)
    Aura_free((Aura_PVoid)pMem);
#else
    ela_free(pMem);
#endif
}

ELAPI_(Boolean) _CMemoryHeap_Trim(MemorySize pad)
{
    return FALSE;
}

ELAPI _CMemoryHeap_GetStatistics(
    MemorySize *pInUseBytes,
    MemorySize *pSystemBytes,
    MemorySize *pMaxSystemBytes)
{
    return E_NOT_IMPLEMENTED;
}

//------------------------------------------------------------------------------

#include "winhack.h"

#define LACKS_FCNTL_H
#define ONLY_MSPACES 1
#define USE_LOCKS 1
#define MSPACES 1

extern "C" ECode __cdecl AuraThread_Sleep(
        /* [in] */ DWORD timeOut,
        /* [out] */ int * pResult);

#define Sleep(timeOut) AuraThread_Sleep(timeOut, NULL)

#include "malloc.c"
